//
// Created by sgyt on 2020/1/19.
// 将获取时间，转换成想要的时间
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>

#define max_size 64
int main(int argc,char *argv[])
{
    time_t t1;
    time(&t1);
    //时间转换成struct tm格式
    struct tm *stTm;
    stTm = gmtime(&t1);
    printf("after gmtime,time is:%4d-%02d-%02d %02d:%02d:%02d\n",
           stTm->tm_year+1900, stTm->tm_mon+1, stTm->tm_mday,
           stTm->tm_hour, stTm->tm_min, stTm->tm_sec);

    stTm = localtime(&t1);
    printf("after localtime,time is:%4d-%02d-%0"
           "2d %02d:%02d:%02d\n",
           stTm->tm_year+1900, stTm->tm_mon+1, stTm->tm_mday,
           stTm->tm_hour, stTm->tm_min, stTm->tm_sec);

    //想time 转换成 字符串
    char str[max_size];
    int res;
    res = strftime(str,sizeof(str),"%F : %R",stTm);
    if(res < 0)
    {
        perror("strftime is error\n");
    }
    printf("time :[%s]\n",str);

    //把字符 转换成 tm
    struct tm tm2;
    memset(&tm2, 0x00, sizeof(struct tm));
    strptime(str,"%F : %R",&tm2);
    printf("time is:%4d-%02d-%0"
           "2d %02d:%02d:%02d\n",
           tm2.tm_year+1900, tm2.tm_mon+1, tm2.tm_mday,
           tm2.tm_hour, tm2.tm_min, tm2.tm_sec);
    return 0;
}
